﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Controller.Paging;
using System.IO;
using TWS.Client;
using System.Data;

public partial class admin_QuanLyBanner : BaseAdminPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            //tab them moi
            LoadData();
            LoadBannerQuangCao();
        }
    }

    private void LoadBannerQuangCao()
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }

        BannerQuangCaoDataSet spDs = new BannerQuangCaoHelper().LoadBannerQuangCao(currentPage, PagingSize, "ThuTu", "ASC",Convert.ToInt32( ddlCon.SelectedValue));

        repSanPham.DataSource = spDs.BannerQuangCao;
        repSanPham.DataBind();

        TotalRecord = new BannerQuangCaoHelper().LoadBannerQuangCaoTheoLoaiSanPham(Convert.ToInt32(ddlCon.SelectedValue)).BannerQuangCao.Rows.Count;

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }

    private void LoadData()
    {
        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPhamCha();
        ddlLoaiCha.DataSource = lspDS.LoaiSanPham;
        ddlLoaiCha.DataTextField = "TenLoaiSanPham";
        ddlLoaiCha.DataValueField = "LoaiSanPhamID";
        ddlLoaiCha.DataBind();

        ddlCha.DataSource = lspDS.LoaiSanPham;
        ddlCha.DataTextField = "TenLoaiSanPham";
        ddlCha.DataValueField = "LoaiSanPhamID";
        ddlCha.DataBind();

        int id = Convert.ToInt32(ddlLoaiCha.SelectedValue);
        LoaiSanPhamDataSet lspDSCon = new LoaiSanPhamHelper().LoadLoaiSanPhamCon(id);
        ddlLoaiCon.DataSource = lspDSCon.LoaiSanPham;
        ddlLoaiCon.DataTextField = "TenLoaiSanPham";
        ddlLoaiCon.DataValueField = "LoaiSanPhamID";
        ddlLoaiCon.DataBind();

        ddlCon.DataSource = lspDSCon.LoaiSanPham;
        ddlCon.DataTextField = "TenLoaiSanPham";
        ddlCon.DataValueField = "LoaiSanPhamID";
        ddlCon.DataBind();
    }

    protected void ddlLoaiCha_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(ddlLoaiCha.SelectedValue);
        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPhamCon(id);
        ddlLoaiCon.DataSource = lspDS.LoaiSanPham;
        ddlLoaiCon.DataTextField = "TenLoaiSanPham";
        ddlLoaiCon.DataValueField = "LoaiSanPhamID";
        ddlLoaiCon.DataBind();
    }

    protected void dllCha_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(ddlCha.SelectedValue);
        LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPhamCon(id);
        ddlCon.DataSource = lspDS.LoaiSanPham;
        ddlCon.DataTextField = "TenLoaiSanPham";
        ddlCon.DataValueField = "LoaiSanPhamID";
        ddlCon.DataBind();
    }

    protected void ddlCon_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        LoadBannerQuangCao();
    }

    public string GetLoaiSanPham(object loaisanphamID)
    {
        if (loaisanphamID != DBNull.Value)
        {
            LoaiSanPhamDataSet lspDS = new LoaiSanPhamHelper().LoadLoaiSanPham(Convert.ToInt32(loaisanphamID));
            return lspDS.LoaiSanPham[0].TenLoaiSanPham;

        }
        return "";
    }

    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid) return;
        BannerQuangCaoHelper spHP = new BannerQuangCaoHelper();
        BannerQuangCaoDataSet spDS = new BannerQuangCaoDataSet();
        BannerQuangCaoDataSet.BannerQuangCaoRow BannerQuangCaomoi = spDS.BannerQuangCao.NewBannerQuangCaoRow();
        BannerQuangCaomoi.TenCongTy = txtTenSanPham.Text;
        if (txtThuTu.Text!="")BannerQuangCaomoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        BannerQuangCaomoi.Link = txtLink.Text;
        BannerQuangCaomoi.LoaiSanPham = Convert.ToInt32(ddlLoaiCon.SelectedValue);
        UploadImage();
        if (txtHinhAnh.Text == string.Empty)
        {
            txtHinhAnh.Text = "noimage.jpg";
        }
        BannerQuangCaomoi.HinhAnh = txtHinhAnh.Text;
        spDS.BannerQuangCao.AddBannerQuangCaoRow(BannerQuangCaomoi);
        spHP.SaveBannerQuangCao(spDS);
        RedirectPage(Request.RawUrl, true);
    }

    private void UploadImage()
    {
        if (txtHinhAnh.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, txtHinhAnh.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            inputFile.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path, fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                txtHinhAnh.Text = string.Empty;
            else
            {
                txtHinhAnh.Text = fn + ".JPG";
            }
        }
    }

    protected void learnMoreButton_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(((Button)sender).CommandArgument);
        BannerQuangCaoDataSet bannerDs = new BannerQuangCaoHelper().LoadBannerQuangCao(id);
        bannerDs.BannerQuangCao[0].Delete();
        new BannerQuangCaoHelper().SaveBannerQuangCao(bannerDs);
        RedirectPage(Request.RawUrl, true);
    }

    protected void repSanPham_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button repCon = ((Button)e.Item.FindControl("learnMoreButton"));
            repCon.Attributes.Add("onclick", "if (!confirm('Xóa dữ liệu?')) return;");
        }
    }

    public string GetLoaiSanPhamName()
    {
        LoaiSanPhamHelper lspHP = new LoaiSanPhamHelper();
        LoaiSanPhamDataSet lspDs = lspHP.LoadLoaiSanPham(Convert.ToInt32(ddlCon.SelectedValue));
        if (lspDs.LoaiSanPham.Rows.Count == 0)
        {
            return "Tất cả";
        }
        LoaiSanPhamDataSet.LoaiSanPhamRow lsp = lspDs.LoaiSanPham[0];
        if (lsp.IsLoaiSanPhamChaIDNull())
        {
            return lsp.TenLoaiSanPham;
        }
        LoaiSanPhamDataSet lspDsCha = lspHP.LoadLoaiSanPham(lsp.LoaiSanPhamChaID);
        LoaiSanPhamDataSet.LoaiSanPhamRow lspCha = lspDsCha.LoaiSanPham[0];

        return lspCha.TenLoaiSanPham + "  >>  " + lsp.TenLoaiSanPham;
    }
}